<?php
class Bahan_keluar_model extends CI_Model {
	private $primary_key = 'id_bahankeluar';
	private $table_name = 'temp_bahan_keluar';

	function __construct(){
		parent::__construct();
	}
	
	function data_getall() {
		$this->db->select('*');
		$this->db->from('bahan_gudang');
		$this->db->join('satuan', 'bahan_gudang.id_satuan = satuan.id_satuan');
		return $this->db->get();
	}
	function getAll() {
		$this->db->select('*');
		$this->db->from('bahan_keluar');
		$this->db->join('bahan_gudang', 'bahan_gudang.id_bahan = bahan_keluar.id_bahan');
		return $this->db->get();
	}
	function cek_id($id) {
		$this->db->where('id_bahan',$id);
		return $this->db->get($this->table_name);
	}
	function count_all(){
		return $this->db->count_all($this->table_name);
	}
	function count_temp_all(){
		return $this->db->count_all('bahan_keluar');
	}	
	function get_by_id($id){
	$this->db->join('satuan', 'bahan_gudang.id_satuan = satuan.id_satuan');
	$this->db->where($this->primary_key,$id);
	return $this->db->get($this->table_name);
	}
	function get_last(){
	$query = "SELECT no_transaksi FROM bahan_keluar WHERE id = (SELECT MAX(id) FROM bahan_keluar) LIMIT 1";
	return $this->db->query($query);
	}
	function save($bahan){
	$this->db->insert($this->table_name,$bahan);
	}
	function fix_save(){
	$query = "insert into bahan_keluar(no_transaksi, id_bahan, tanggal, jumlah) select no_transaksi, id_bahan, tanggal, jumlah from temp_bahan_keluar";
	$this->db->query($query);
	}
	function distinct_table(){
	$query = ("SELECT DISTINCT(bahan_keluar.id_bahan) as id_bahan, stok FROM bahan_keluar INNER JOIN bahan_gudang ON bahan_gudang.id_bahan=bahan_keluar.id_bahan");
//	echo $query;die();
	return $this->db->query($query);
	}
	function delete_all(){
	$this->db->truncate($this->table_name);
	}
	function jumlah($id){
	$query = ("SELECT SUM(jumlah) as jumlah FROM bahan_keluar WHERE id_bahan='$id'");
	return $this->db->query($query)->row_array();
	}
	function update($id,$bahan){
	$this->db->where($this->primary_key,$id);
	$this->db->update($this->table_name,$bahan);
	}
	
	function delete($id){
	$this->db->where($this->primary_key,$id);
	$this->db->delete($this->table_name);
	}
}